<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>exec</title>
</head>
<body>
        <script>
            var reg = /[0-9]+/g;
            var s = '我今天24岁了，我有2个山芋，还有5个糖';
            var arr = [];
           function foo(reg,s) {
               var tmp = reg.exec(s);
               if(tmp){
                   arr.push(tmp[0]);
                   foo(reg,s)
               }
           }
           foo(reg,s);
            console.log(arr)
        </script>
</body>
</html>